package com.ruoyi.web.controller.api;

import com.ruoyi.mall.domain.MallTimeConfig;
import com.ruoyi.mall.domain.MallTimeOrder;
import com.ruoyi.mall.service.IMallDistributionConfigService;
import com.ruoyi.mall.service.IMallTimeConfigService;
import com.ruoyi.mall.service.IMallTimeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时任务
 *
 * @autor LCL
 * @date - 2022/03/03
 */
@Component
public class TimeTask {

    @Autowired
    private IMallTimeOrderService mallTimeOrderService;
    @Autowired
    private IMallTimeConfigService commonTimeConfigService;
    @Autowired
    private IMallDistributionConfigService mallDistributionConfigService;


    //编写具体的定时任务组件（@Component注解），并且在需要定时调度的方法上添加@Scheduled触发器
    //每隔5秒执行一次：*/5 * * * * ?
    //每隔1分钟执行一次：0 */1 * * * ?
    //每天23点执行一次：0 0 23 * * ?
    //从0时开始，每小时执行一次：0 0 0/1 * * ?
    //从0时开始，每2小时执行一次：0 0 0/2 * * ?
    //每天凌晨1点执行一次：0 0 1 * * ?
    //每月1号凌晨1点执行一次：0 0 1 1 * ?
    //每月最后一天23点执行一次：0 0 23 L * ?
    //每周星期天凌晨1点实行一次：0 0 1 ? * L
    //在26分、29分、33分执行一次：0 26,29,33 * * * ?
    //每天的0点、13点、18点、21点都执行一次：0 0 0,13,18,21 * * ?
//    @Scheduled(cron = "0 */1 * * * ?")
    public void startUp() throws ParseException {
        // 定时取消订单(抢购商品)
        MallTimeConfig timeConfig = commonTimeConfigService.findMallTimeConfig();
        if (timeConfig.getTimingCancel() != 0) {
            System.out.println("取消订单");
            MallTimeOrder cancelOrder = new MallTimeOrder();
            cancelOrder.setStatus(1);
            List<MallTimeOrder> cancelOrderList = mallTimeOrderService.selectMallTimeOrderList(cancelOrder);
            for (MallTimeOrder COL : cancelOrderList) {
                Date nowTime = new Date();
                Calendar orderTime = Calendar.getInstance();
                orderTime.setTime(COL.getCreateTime());
                orderTime.add(Calendar.MINUTE, timeConfig.getTimingCancel());
                if(nowTime.getTime() >= orderTime.getTime().getTime()) {
                    // 取消订单
                    mallTimeOrderService.cancelOrderByUserIdOrderId(COL.getUserId(), COL.getId());
                }
            }
        }
        // 卖方自动X分钟确认收款
        if (timeConfig.getConfirmTiming() != 0) {
            System.out.println("确认收款");
            MallTimeOrder confirmOrder = new MallTimeOrder();
            confirmOrder.setStatus(2);
            List<MallTimeOrder> confirmOrderList = mallTimeOrderService.selectMallTimeOrderList(confirmOrder);
            for (MallTimeOrder COL : confirmOrderList) {
                Date nowTime = new Date();
                Calendar orderTime = Calendar.getInstance();
                orderTime.setTime(COL.getCreateTime());
                orderTime.add(Calendar.MINUTE, timeConfig.getConfirmTiming());
                if(nowTime.getTime() >= orderTime.getTime().getTime()) {
                    // 确认收款
                    mallTimeOrderService.confirmThePayment(COL.getUserId(), COL.getId());
                }
            }
        }
    }

}
